﻿Imports SecretaryToolData

Public Class frmEnterTime

	Private ReadOnly Property TypedPublisherDataBindingSource As List(Of VM_PublisherInformation)
		Get
			Return CType(lstPublishers.DataSource, List(Of VM_PublisherInformation))
		End Get
	End Property

	Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
		Me.Close()
	End Sub

	Private Sub frmEnterTime_Load(sender As Object, e As System.EventArgs) Handles Me.Load
		PopulatePublishers(Nothing, Nothing)
	End Sub

	Private Sub btnEnterTime_Click(sender As System.Object, e As System.EventArgs) Handles btnEnterTime.Click
		EnterTime()
	End Sub

	Private Sub EnterTime()
		If lstPublishers.SelectedValue IsNot Nothing Then
			Dim myPublisherCard As New frmPublisherCard(lstPublishers.SelectedValue)

			myPublisherCard.ShowDialog()
			PopulatePublishers(Nothing, Nothing)

			lstPublishers.Focus()
		End If
	End Sub

	Private Sub lstPublishers_DoubleClick(sender As Object, e As System.EventArgs) Handles lstPublishers.DoubleClick
		EnterTime()
	End Sub

	Private Sub lstPublishers_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles lstPublishers.KeyDown
		If e.KeyCode = Windows.Forms.Keys.Enter Then
			EnterTime()
		End If
	End Sub

	Private Sub PopulatePublishers(sender As Object, e As System.EventArgs) Handles rdoAllPublishers.CheckedChanged, rdoUnreportedPublishers.CheckedChanged
		'only execute this code if we are selecting a radio button
		'don't execute if if we are unselecting it
		'or execute it if we are refreshing the data on the form getting focus back
		If (sender IsNot Nothing AndAlso CType(sender, RadioButton).Checked) OrElse sender Is Nothing Then
			Dim publisherCollection As New List(Of VM_PublisherInformation)
			Select Case True
				Case rdoAllPublishers.Checked
					publisherCollection = PublisherData.LocalPublishers.OrderBy(Function(x) x.FullName).ToList

				Case rdoUnreportedPublishers.Checked
					publisherCollection = PublisherData.ActivePublishersMissingReports.OrderBy(Function(x) x.FullName).ToList

			End Select

			lstPublishers.DisplayMember = Extensions.GetMemberInfo(Function(x As VM_PublisherInformation) x.FullName).Name
			lstPublishers.ValueMember = Extensions.GetMemberInfo(Function(x As VM_PublisherInformation) x.PublisherID).Name
			lstPublishers.DataSource = publisherCollection.Select(Function(x) New VM_PublisherInformation(x.PublisherID)).ToList
		End If
	End Sub

	Private Sub btnSendReminders_Click(sender As System.Object, e As System.EventArgs) Handles btnSendReminders.Click
		For Each publisher In TypedPublisherDataBindingSource
			If (New VM_PublisherCardRecord(publisher.PublisherID, Now.AddMonths(-1).FirstOfTheMonth)).Hours = 0 Then

				Dim myPublisher As New VM_PublisherInformation(publisher.PublisherID)
				Dim myGroupOverseer As New VM_PublisherInformation(myPublisher.GroupOverseerID)
				Dim myGroupAssistant As New VM_PublisherInformation(myPublisher.GroupAssistantID)

				Dim newMessage As New MailToLinkBuilder
				Dim subject As String = String.Empty
				Dim body As String = String.Empty

				Select Case True
					Case rdoReminderGroupOverseers.Checked
						subject = String.Format("Please collect the Field Service time of {0}.", myPublisher.FullName)
						body = String.Format("{1},{0}{0}Please collect the field service time for the month of {2} from {3}. Thanks!{0}{0}Your Brother,{0}Congregation Secretary", Environment.NewLine, myGroupOverseer.FullName, Now.AddMonths(-1).ToString("MMMM, yyyy"), myPublisher.FullName)

						If myGroupOverseer.Email <> "" Then
							newMessage.ToAddresses.Add(myGroupOverseer.Email)
						End If

						If myGroupAssistant.Email <> "" Then
							newMessage.CCAddresses.Add(myGroupAssistant.Email)
						End If
					Case rdoReminderPublishers.Checked
						subject = "Please turn in your Field Service time."
						body = String.Format("{1},{0}{0}Please turn in your field service time for the month of {2}. Thanks!{0}{0}Your Brother,{0}Congregation Secretary", Environment.NewLine, myPublisher.FullName, Now.AddMonths(-1).ToString("MMMM, yyyy"))

						If myPublisher.Email <> "" Then
							newMessage.ToAddresses.Add(myPublisher.Email)
						End If

						If myPublisher.TextMessageEmail <> "" Then
							newMessage.ToAddresses.Add(myPublisher.TextMessageEmail)
						End If

						'even messages to publishers should copy the group overseer
						If myGroupOverseer.Email <> "" Then
							newMessage.CCAddresses.Add(myGroupOverseer.Email)
						End If

						If myGroupAssistant.Email <> "" Then
							newMessage.CCAddresses.Add(myGroupAssistant.Email)
						End If
				End Select

				newMessage.Subject = subject
				newMessage.Body = body

				Process.Start(newMessage.GetMailToLink())
			End If

		Next

	End Sub

	Private Sub txtFilter_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtFilter.KeyDown
		If e.KeyCode = Windows.Forms.Keys.Enter Then
			EnterTime()
		End If
	End Sub

	Private Sub txtFilter_TextChanged(sender As Object, e As System.EventArgs) Handles txtFilter.TextChanged
		lstPublishers.FilterString = txtFilter.Text
	End Sub

	Private Sub lstPublishers_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles lstPublishers.KeyPress
		txtFilter.Text = e.KeyChar
		txtFilter.Focus()
		txtFilter.SelectionStart = txtFilter.TextLength
	End Sub
End Class